package com.njlime.sys.util;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by Dean on 2017/4/18.
 */
public class NoteSendUtil {

    private static final Logger logger = Logger.getLogger(NoteSendUtil.class);
    //官网的URL
    private static final String MESSAGE_URL = "http://gw.api.taobao.com/router/rest";
    //成为开发者，创建应用后系统自动生成
    private static final String APP_KEY = "24649508";
    private static final String APP_SECRET = "3cf7b6de441a23f84a91f60f0af3b375";
    //短信相关设置
    private static final String SMS_TEMPLATE = "SMS_102155069";
    private static final String SMS_SIGN = "小清柠";
    public static final int SEND_PERIOD = 60;



    /**
     * 发送验证码
     * @param mobileNum
     * @param noteCode
     * @return
     */
    public static boolean verifyNote(String mobileNum, String noteCode) {
        String json = "{randomNumber:'" + noteCode + "'}";
        TaobaoClient client = new DefaultTaobaoClient(MESSAGE_URL, APP_KEY, APP_SECRET);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setSmsType("normal");
        req.setSmsFreeSignName(SMS_SIGN);
        req.setSmsParamString(json);
        req.setRecNum(mobileNum);
        req.setSmsTemplateCode(SMS_TEMPLATE);
        req.setExtend("");
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            logger.debug(rsp.getBody());
            return true;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return false;
        }
    }

    /**
     * 获取随机验证码
     * @param charCount
     * @return
     */
    public static String getRandNum(int charCount) {
        Random random = new Random();
        int x = random.nextInt(899999);
        x = x+100000;
        return x+"";
    }
}